package MediaEntity;

import java.io.File;
import java.util.*;

import javafx.scene.media.Media;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.File;
import java.io.FilenameFilter;

public class MediaList {
    private LinkedList<MediaEntity> mediaEntityList;
    /**某一目录的路径*/
    private String path;
    private String type;

    public int size(){
        return mediaEntityList.size();
    }

    public void init(String path,String type){
//        LinkedList<MediaEntity> local;
//        if(mediaEntityList.size() != 0)
//            local = new LinkedList<MediaEntity>(mediaEntityList);
        LinkedList<String> linkedList = MediaList.detectFile(path, type);
        init_MediaList(linkedList,type);
    }

    //探查某一目录下文件
    public static LinkedList<String> detectFile(String path,String type){
        LinkedList<String> t= new LinkedList<>();
        File dir = new File(path);
        File[] subFiles = dir.listFiles();
        try{
            for (File subFile : subFiles) {
                if(subFile.isFile() && subFile.getName().endsWith("."+type))
                    t.add(dir+"\\"+subFile.getName());
            }
        } catch (NullPointerException e) {
            System.out.println("No such files in this directory!");
            //或者做一些其他事情，比如弹出窗口之类的
        }
        return t;
    }

    public MediaList(){
        mediaEntityList = new LinkedList<>();
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setType(String str){
        type = str;
    }

    private void init_MediaList(LinkedList<String> fileList,String type){
        //这里的filelist内部必须是绝对路径，使用上方的静态方法返回的链表即可
        this.type=type;
        if(Objects.equals(this.type, "mp3")){
            try{
                for(String str :fileList){
                    this.mediaEntityList.add(new MP3Entity(str));
                }
            }catch(NullPointerException e){
                e.printStackTrace();
            }
        }else if(Objects.equals(type, "mp4")){
                try{
                for(String str :fileList){
                    this.mediaEntityList.add(new MP4Entity(str));
                }
            }catch(NullPointerException e){
                e.printStackTrace();
            }
        }
    }

    public Media getMedia(int t){
        return this.mediaEntityList.get(t).getMedia();
    }

    public Media getRandomMedia(int index){
        Random t=new Random();
        int i;
        do{
            i=t.nextInt(this.mediaEntityList.toArray().length);
        }while(index==i);
        return this.mediaEntityList.get(i).getMedia();
    }

    public void sortByName(){
        this.mediaEntityList.sort(new Comparator<MediaEntity>() {
            @Override
            public int compare(MediaEntity o1, MediaEntity o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
    }

    public int getIndex(Media media){
        int res = -1;
        for(int i=0;i< mediaEntityList.size();i++){
            Media mi = getMedia(i);
            if(mi.toString().equals(media.toString())) {
                res = i;
                break;
            }
        }
        return res;
    }

    public LinkedList<MediaEntity> getMediaEntityList() {
        return mediaEntityList;
    }
}

